package com.sam.demo.spring.boot.mybatis.service;

import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sam.demo.spring.boot.mybatis.entity.User;
import com.sam.demo.spring.boot.mybatis.entity.mapper.UserMapper;

@Service
public class UserService {

	@Autowired
	private UserMapper userMapper;

	@Autowired
	@Qualifier("batchSqlSessionTemplate")
	private SqlSessionTemplate batchSqlSessionTemplate;

	@Autowired
	@Qualifier("simpleSqlSessionTemplate")
	private SqlSessionTemplate simpleSqlSessionTemplate;

	@Transactional
	public User saveUser(User user){
		System.out.println(batchSqlSessionTemplate.getExecutorType());
		System.out.println(simpleSqlSessionTemplate.getExecutorType());


		user.setName("aaa");
		userMapper.insert(user);

		user.setName("bbb");
		userMapper.insert(user);
		if(1==1){
			throw new RuntimeException();
		}
		return user;
	}
	
	@Transactional
	public User findOne(Integer id){
		return userMapper.selectByPrimaryKey(id);
	}
	
	@Transactional
	public User deleteUser(Integer id){
		User user = userMapper.selectByPrimaryKey(id);
		userMapper.deleteByPrimaryKey(id);
		return user;
	}
}
